<script lang="ts" setup name="Goods">
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'

import { computed, ref, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import useStore from '@/store'
import { storeToRefs } from 'pinia'
import { Skus } from '@/types/data'
import Message from '@/components/message'
import { CartItem } from '@/types/types/cart'
// import itemVue from '@/components/bread/item.vue'
const route = useRoute()
const { goods, cart, user } = useStore()
// 商品默认数量
const number = ref(1)
// 商品默认选中的规格
const skuId = ref('')

// const goodsName = ref<string[]>([])
watchEffect(async () => {
  const id = route.params.id as string
  if (route.fullPath === `/goods/${id}`) {
    await goods.getGoodsInfo(id)
    // 商品名称
    // const name = goods.goodsInfo.skus.filter(item => item.inventory > 0)[0].specs.map(item => item.valueName)
    // goodsName.value = name
    skuId.value = goods.goodsInfo.skus.filter(item => item.inventory > 0)[0].id
  }
})
//
// const checked = computed(() => goodsInfo.skus.filter((item: Skus) => item.inventory > 0)?.[0]?.id)
const { goodsInfo } = storeToRefs(goods)
const checkGoods = (val: string) => {
  // console.log(val)
  skuId.value = val
}

const addCar = () => {
  // 判断是否是完整的sku
  if (!skuId.value) return Message.warning('请选择完整商品')
  // console.log('添加购物车')
  if (user.userInfo.token) {
    cart.addCart({ skuId: skuId.value, count: number.value })
  } else {
    // 添加到本地存储
    // console.log('本地')
    const sku = goodsInfo.value.skus.find(item => item.id === skuId.value)!
    const attrsText = sku.specs.map(item => item.name + ':' + item.valueName).join(' ')
    cart.addCart({
      // 本地添加
      id: goodsInfo.value.id,
      name: goodsInfo.value.name,
      picture: goodsInfo.value.mainPictures[0],
      price: goodsInfo.value.price,
      count: number.value,
      skuId: skuId.value,
      attrsText: attrsText,
      selected: true,
      nowPrice: goodsInfo.value.price,
      stock: goodsInfo.value.inventory,
      isEffective: true
    } as CartItem)
  }
  Message.success('添加成功')
}
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <template v-if="goodsInfo.categories">
          <XtxBreadItem :to="`/category/${goodsInfo.categories[1].id}`">{{ goodsInfo.categories[1].name }}</XtxBreadItem>
          <XtxBreadItem :to="`/category/sub/${goodsInfo.categories[0].id}`">{{ goodsInfo.categories[0].name }}</XtxBreadItem>
          <XtxBreadItem>{{ goodsInfo.name }}</XtxBreadItem>
        </template>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="dedia">
          <GoodsImage :images="goodsInfo.mainPictures" />
          <GoodsSales />
        </div>

        <div class="spec">
          <GoodsName :goods="goodsInfo" />
          <!-- :key="goodsInfo.id" -->
          <!-- 增加key属性,是因为从一个商品跳转到另一个商品的时候, 页面没有变 商品信息不刷新   =>组件缓存 
          skuId="1369155864430120962"
          :skuId="goodsInfo.skus?.[0].id"
           props.goods.skus.filter(item => item.inventory > 0)
           :skuId="goodsInfo.skus?.filter(item => item.inventory > 0)?.[0]?.id"
          -->
          <GoodsSku v-if="goodsInfo" :goods="goodsInfo" :skuId="goodsInfo.skus?.filter(item => item.inventory > 0)?.[0]?.id" @checkGoods="checkGoods" />

          <XtxNumbox v-model:number="number" />
          <XtxButton class="button" type="primary" size="middle" @click="addCar">加入购物车</XtxButton>
        </div>
      </div>

      <!--  -->
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail :goods="goodsInfo" />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  padding: 30px;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 0 30px;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.button {
  margin-top: 20px;
}
</style>
